package com.ECPDemo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.ECPDemo.contant.OrderResultContant;
import com.ECPDemo.contant.OrderStatusContant;
import com.ECPDemo.mapper.OrderItemMapper;
import com.ECPDemo.mapper.OrderMapper;
import com.ECPDemo.pojo.ECPResult;
import com.ECPDemo.pojo.Order;
import com.ECPDemo.pojo.OrderInfo;
import com.ECPDemo.pojo.OrderItem;
import com.ECPDemo.pojo.OrderShipping;
import com.ECPDemo.service.OrderService;
import com.github.pagehelper.PageHelper;

@Service
public class OrderServiceImpl implements OrderService {
	
	
	
	Log log = LogFactory.getLog(OrderServiceImpl.class);
	@Resource
	private OrderMapper orderMapper;
	
	@Resource
	private OrderItemMapper orderItemMapper;

	@Override
	public ECPResult createOrder(List<OrderItem> items,OrderShipping orderShipping,String payment) {
		long result = 0; 
		if(items.size() < 1){
			log.info("createOrder ---------> orderItem size is  0  ");
			result = OrderResultContant.ITEM_IS_NULL.getId();
		}
		for (OrderItem orderItem : items) {
			if(orderItem.getNum() == 0 || orderItem.getNum() == null ){
				log.info("createOrder ---------> item :" + orderItem.getTitle() + "order Num is null or 0  num : " + orderItem.getNum());
				result = OrderResultContant.ITEM_NUM_IS_ZERO.getId();
			}
		}
		
		//1.生成订单
		Order order = new Order(OrderStatusContant.UNPAID.getId(),new Date(),1L);
		order.setPayment(payment);
		order.setBuyerNick("Kevin");
		log.info("createOrder ---- order.insertReturnKey -----> order : " + order.toString());
		orderMapper.insertReturnKey(order);
		//2.商品订单产生关联
		
		for (OrderItem orderItem : items) {
			orderItem.setOrderId(order.getOrderId());
			orderItem.setTotalFee(orderItem.getPrice() * orderItem.getNum());
			log.info("createOrder ---- orderItem.insert -----> orderItem : " + orderItem.toString());
			orderItemMapper.insert(orderItem);
		}
		result = order.getOrderId();
		return ECPResult.ok(result);
	}

	@Override
	public List<OrderInfo> getOrderList(Order order,Integer page, Integer rows) {
		//分页处理
		PageHelper.startPage(page, rows);
		log.info("getOrderList -----> parameter : " + order.toString() + "page : " + page + " rows : " + rows);
		
		ArrayList<OrderInfo> orderInfoList = new ArrayList<OrderInfo>();
		
		List<Order> orderList = orderMapper.queryOrderList(order);
		log.info("getOrderList ------> orderList : " + orderList.toString());
		for (Order orderResult : orderList) {
			List<OrderItem>  orderItemList = orderItemMapper.queryOrderItemByOrderId(orderResult.getOrderId().toString());
			OrderInfo orderInfo = new OrderInfo(orderResult);
			orderInfo.setOrderItems(orderItemList);
			orderInfoList.add(orderInfo);
		}
		
		log.info("getOrderList ------> orderInfoList : " + orderInfoList.toString());
		
		return orderInfoList;
	}

	@Override
	public Integer getOrderListCount(Order order) {
		log.info("getOrderListCount ------> order : " + order.toString());
		return orderMapper.queryOrderListCount(order);
	}

}
